{% extends "advbase.html" %}
{% block advcontent -%}
<fieldset>
    <legend>{{_("Deliver now")}}</legend>
    <p style="font-size:0.8em;color:grey;">
        {{_("Deliver selected books now.")}}
    </p>
  {% if booksnum > 0 %}
    <a href="/deliver?u={{nickname}}" value="{{_("Deliver")}}" id="deliverButton" 
        class="pure-button pure-button-primary" style="margin-right:4em;" >{{_("Deliver")}}</a>
  {% if booksnum == 1 and books[0].builtin == False -%}
    <a href="#" class="actionButton" onclick="SelectAll();UpdateDeliverFeedLink();return false;">{{_("Select all")}}</a>
    <a href="#" class="actionButton" onclick="SelectNone();UpdateDeliverFeedLink();return false;">{{_("Select none")}}</a>
    <hr style="width:90%;color:silver;margin:1em auto;" />
    <div class="pure-control-group">
        {% for item in books[0].feeds %}
        <label for="{{item.key().id()}}" class="pure-checkbox">
            <input id="{{item.key().id()}}" bookid="{{books[0].key().id()}}" type="checkbox" checked="1" onclick="UpdateDeliverFeedLink();" />{{item.title}}
        </label><br />
        {% endfor %}
    </div>
  {% else -%}
    <a href="#" class="actionButton" onclick="SelectAll();UpdateDeliverLink();return false;">{{_("Select all")}}</a>
    <a href="#" class="actionButton" onclick="SelectNone();UpdateDeliverLink();return false;">{{_("Select none")}}</a>
    <hr style="width:90%;color:silver;margin:1em auto;" />
    <div class="pure-control-group">
        {% for item in books %}
        <label for="{{item.key().id()}}" class="pure-checkbox">
            <input id="{{item.key().id()}}" type="checkbox" checked="1" onclick="UpdateDeliverLink();" />{{item.title}}
        </label><br />
        {% endfor %}
    </div>
  {% endif %}
    
  {% else %}
    <hr style="width:90%;color:silver;margin:1em auto;" />
    {{_("Do not have any subscribed book.")}}
  {% endif %}
</fieldset>
{% endblock -%}
{% block javascriptinhead %}
<script type="text/javascript">
    //Update link of deliver button for feeds (custom rss)
    function UpdateDeliverFeedLink() {
        var feedsId = [];
        var inputs = document.getElementsByTagName("input");
        var bookId = "";
        var deliverButton = document.getElementById("deliverButton");
        var inputsNum = inputs.length;
        for (var i = 0; i < inputsNum; i++) {
            if (bookId == "") {
                bookId = inputs[i].attributes["bookid"].value;
            }
            if ((inputs[i].type == "checkbox") && inputs[i].checked) {
                feedsId.push(inputs[i].attributes["id"].value);
            }
        }
        var feedsNum = feedsId.length;
        if ((feedsNum > 0) && (feedsNum != inputsNum)) {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + bookId + "&feedsId=" + feedsId.join("|");
        } else {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + bookId;
        }
    }

    //Update link of deliver button for books
    function UpdateDeliverLink() {
        var booksId = [];
        var inputs = document.getElementsByTagName("input");
        var deliverButton = document.getElementById("deliverButton");
        var inputsNum = inputs.length;
        for (var i = 0; i < inputsNum; i++) {
            if ((inputs[i].type == "checkbox") && inputs[i].checked) {
                booksId.push(inputs[i].attributes["id"].value);
            }
        }
        var booksNum = booksId.length;
        if ((booksNum > 0) && (booksNum != inputsNum)) {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + booksId.join("|");
        } else {
            deliverButton.href = "/deliver?u={{nickname}}";
        }
    };

    function SelectAll() {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox") {
                inputs[i].checked = true;
            }
        }
    };

    function SelectNone() {
        var inputs = document.getElementsByTagName("input");
        var deliverButton = document.getElementById("deliverButton");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox") {
                inputs[i].checked = false;
            }
        }
    };
</script>
{% endblock %}
